var movingCount = function (m, n, k) {
  let sum = (x) => (x % 10) + Math.floor(x / 10);
  let flag = [];
  move(0, 0, m, n, k, flag);
  return flag.filter((item) => item === true).length;

  function move(i, j, m, n, k, flag) {
    if (
      i < 0 ||
      j < 0 ||
      i > m - 1 ||
      j > n - 1 ||
      sum(i) + sum(j) > k ||
      flag[i + j * m] == true
    )
      return;
    flag[i + j * m] = true;
    move(i - 1, j, m, n, k, flag);
    move(i + 1, j, m, n, k, flag);
    move(i, j - 1, m, n, k, flag);
    move(i, j + 1, m, n, k, flag);
  }
};
